import Dialog from '../../vant-weapp/dialog/dialog';
import Toast from '../../vant-weapp/toast/toast';
import fw from '../../core/framework.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    type: 'create',
    device: {
      name: '',
      code: '',
      alarm_contact_group_id: null,
      remark: ''
    },
    alarmContactGroupData: [],
    currAlarmContactGroup: {},
    showAlarmContactGroup: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var type = options.type;
    console.log('options---->',options)
    this.setData({
      type: type
    });
    wx.setNavigationBarTitle({
      title: this.data.type == 'create' ? '创建设备' : '编辑设备'
    });
    if (type == 'edit') {
      console.log('options',options)
      this.setData({
        device: JSON.parse(options.device)
      })
    }
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.initAlarmContactGroup();
  },
  initAlarmContactGroup() {
    var self = this;
    fw.ajax('enumeration/alarmContactGroup').then(res => {
      if (res.data) {
        self.setData({
          'alarmContactGroupData': res.data
        })
      }
       if (self.data.type == 'create') {
        self.setData({
          'currAlarmContactGroup': self.data.alarmContactGroupData[0],
          ['device.alarm_contact_group_id']: self.data.alarmContactGroupData[0].value
        })
      } else {
        let currGroupId = (self.data.device.alarm_contact_group_id[0]||{}).alarm_contact_group_id;
        console.log(currGroupId);
        self.data.alarmContactGroupData.forEach(d => {
          if (d.value == currGroupId) {
            self.setData({
              currAlarmContactGroup: d,
              ['device.alarm_contact_group_id']:currGroupId
            })
          }
        })
      }
    })
  },
  closeAlarmContactGroup() {
    this.setData({
      showAlarmContactGroup: false
    })
  },
  openAlarmContactGroup() {
    this.setData({
      showAlarmContactGroup: true
    })
  },
  chooseAlarmContactGroup(event) {
    var _value = event.detail;
    this.setData({
      showAlarmContactGroup: false,
      currAlarmContactGroup: _value,
      ['device.alarm_contact_group_id']: _value.value
    });
  },
  setName: function (event) {
    this.setData({
      ['device.name']: event.detail
    });
  },
  setCode: function (event) {
    this.setData({
      ['device.code']: event.detail
    });
  },
  setRemark: function (event) {
    this.setData({
      ['device.remark']: event.detail
    });
  },
  cancel() {
    fw.url.back();
  },
  save: function () {
    let self = this;
    var ajaxData = self.data.device;
    if (!ajaxData.name) {
      Toast.fail('设备名称不能为空')
      return false;
    }
    if (!ajaxData.code) {
      Toast.fail('设备编号不能为空')
      return false;
    }
    if (this.data.type == 'create') {
      fw.ajax('device/create', ajaxData).then(res => {
        if (res.code == 0) {
          Toast.success('创建设备成功');
          self.cancel();
        } else {
          Toast.fail(res.msg);
        }
      })
    } else {
      fw.ajax('device/update', ajaxData).then(res => {
        if (res.code == 0) {
          Toast.success('更新设备成功');
          self.cancel();
        } else {
          Toast.fail(res.msg);
        }
      })
    }
  }
})